package com.ewiqi.springsecuritydemo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * @author fengyu.zhang
 * @date 2021/1/18 5:35 下午
 */
@Configuration
public class MySecurityAdapter extends WebSecurityConfigurerAdapter {
    @Resource
    UserDetailsService userService;

    @Resource
    PasswordEncoder passwordEncoder;

    @Bean
    PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .anyRequest().permitAll()
                .and()
                .csrf().disable()
                .formLogin()
                    .defaultSuccessUrl("/loginSuccess")
                    .failureForwardUrl("/loginFailed")
                    .permitAll();
    }
}
